<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="选择公司代码">
          <j-dict-select-tag v-decorator="['companyCode', validatorRules.partnerId]" placeholder="请选择公司代码"
                             :triggerChange="true"
                             dictCode="stm_store_info,distinct partner_id,partner_id,status!=0"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="设置起订数量">
          <a-input-number v-decorator="[ 'quantity', validatorRules.quantity]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否启用">
          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="status"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import {httpAction} from '@/api/manage'
import pick from 'lodash.pick'
import moment from "moment"

export default {
  name: "PmsProductMinimumQuantityModal",
  data() {
    return {
      title: "操作",
      visible: false,
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      productId: "",
      status: true,
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        quantity: {rules: [{required: true, message: '请填写起订数量!'}]},
        companyCode: {rules: [{required: true, message: '请选择公司代码!'}]},
      },
      url: {
        add: "/pms/pmsProductMinimumQuantity/add",
        edit: "/pms/pmsProductMinimumQuantity/edit",
      },
    }
  },
  created() {
  },
  methods: {
    add(productId) {
      this.status = true;
      this.productId = productId;
      this.edit("");
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      if(record!=""){
        this.status = record.status == 1 ? true : false;
      }
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'companyCode', 'quantity'))
      });

    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      const that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          formData.productId = this.productId;
          formData.status = this.status ? 1 : 0;
          console.log(formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },


  }
}
</script>

<style scoped>
@import '~@assets/less/common.less'
</style>